
using System;
using System.Text;
using SpotifyGenNHibernate.CEN;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Exceptions;
using SpotifyGenNHibernate.EN.Spotify;

namespace SpotifyGenNHibernate.CAD.Spotify
{
        public partial class NoticiaCAD : BasicCAD, INoticiaCAD
        {
        public NoticiaCAD() : base ()
        {
        }

        public NoticiaCAD(ISession sessionAux) : base (sessionAux)
        {
        }



        public NoticiaEN ReadOIDDefault (int id)
        {
                NoticiaEN noticiaEN = null;

                try
                {
                        SesssionInitializeTransaction ();
                        noticiaEN = (NoticiaEN)session.Load (typeof(NoticiaEN),
                                id

                                );
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in NoticiaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return noticiaEN;
        }


        public int New_ (NoticiaEN noticia)
        {
                try
                {
                        SesssionInitializeTransaction ();

                        session.Save (noticia);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in NoticiaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return noticia.Id;
        }

        public void Destroy (int id)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        NoticiaEN noticiaEN = (NoticiaEN)session.Load (typeof(NoticiaEN),
                                id

                                );
                        session.Delete (noticiaEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in NoticiaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }

        public void Edit (NoticiaEN noticia)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        NoticiaEN noticiaEN = (NoticiaEN)session.Load (typeof(NoticiaEN), noticia.Id);

                        noticiaEN.Titulo = noticia.Titulo;


                        noticiaEN.Descripcion = noticia.Descripcion;


                        noticiaEN.Enlace = noticia.Enlace;


                        noticiaEN.FechaPublicacion = noticia.FechaPublicacion;

                        session.Update (noticiaEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in NoticiaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }
        public System.Collections.Generic.IList<NoticiaEN> Consultar (int first, int size)
        {
                System.Collections.Generic.IList<NoticiaEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        if (size > 0)
                                result = session.CreateCriteria (typeof(NoticiaEN)).
                                         SetFirstResult (first).SetMaxResults (size).List<NoticiaEN>();
                        else
                                result = session.CreateCriteria (typeof(NoticiaEN)).List<NoticiaEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in NoticiaCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }
        }
}
